#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 0.3e6 + 5;
const int BASE = 0.9e6 + 5;
const int MX = 1.2e6 + 10;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
char s[N];
int bit[MX + 5];
int lowbit(int x) { return x & -x; }
void update(int x, int d) {
  for (int i = x; i <= MX; i += lowbit(i)) bit[i] += d;
}
int query(int x) {
  int res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += bit[i];
  return res;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  ll ans = 0;
  char c;
  int cnt = BASE;
  update(cnt, 1);
  rep(i, 1, n) {
    cin >> c;
    if (c == '0')
      cnt++;
    else
      cnt -= 3;
    ans += query(cnt + 1) - query(cnt);
    ans += query(cnt - 2);
    update(cnt, 1);
  }
  cout << ans;
  return 0;
}